<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">后台管理系统</div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="ms-content">
        <el-form-item prop="username">
          <el-input v-model.trim="loginForm.username" placeholder="username">
            <el-button slot="prepend" icon="el-icon-user" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model.trim="loginForm.password" type="password" placeholder="password">
            <el-button slot="prepend" icon="el-icon-lock" />
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-input v-model.trim="loginForm.captcha" @keyup.enter.native="login">
            <el-button slot="prepend" icon="el-icon-key" />
            <el-image slot="append" style="width: 80px; height: 30px" :src="captchaInfo.base64" fit="cover" @click="getCaptcha" />
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
        <p class="login-tips">ICP : <a href="http://beian.miit.gov.cn" target="_blank"> 粤ICP备20039585号-1</a></p>
      </el-form>
    </div>
  </div>
</template>

<script>
import { captcha } from '@/api/login'
export default {
  name: 'Login',
  components: {},
  props: {},
  data () {
    return {
      captchaInfo: {},
      loginForm: {
        username: '',
        password: '',
        captcha: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getCaptcha()
  },
  mounted () {},
  methods: {
    // 刷新验证码
    getCaptcha: async function () {
      const res = await captcha()
      this.captchaInfo = res
    },
    // 登陆
    login: async function () {
      const data = {
        username: this.loginForm.username,
        password: this.loginForm.password,
        captcha: this.loginForm.captcha,
        captchaKey: this.captchaInfo.key
      }
      const res = await this.$store.dispatch('login', data)
      if (res) {
        this.$router.push('/home')
        this.$message({
          message: '登陆成功',
          type: 'success',
          duration: 1000
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.login-wrap {
  position: relative;
  width:100%;
  height:100%;
  background-image: url(~@/assets/img/login-bg.jpg);
  background-size: 100%;
}
.ms-title{
  width:100%;
  line-height: 50px;
  text-align: center;
  font-size:20px;
  color: #000;
  border-bottom: 1px solid #ddd;
}
.ms-login{
  position: absolute;
  left:50%;
  top:50%;
  width:350px;
  margin:-190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255,255,255, 1);
  overflow: hidden;
}
.ms-content{
  padding: 30px 30px;
}
.login-btn{
  text-align: center;
}
.login-btn button{
  width:100%;
  height:36px;
  margin-bottom: 15px;
}
.login-tips{
  font-size:12px;
  line-height: 12px;
}
</style>
